#coding:utf-8

from encSear import encSearBase
import pycurl
import re
__metaclass__=type #新式类

class c0llision(encSearBase):
  def __init__(self):
    super(c0llision,self).__init__(url='http://www.c0llision.net/webcrack/request',
                 patt=r'(?<=<td class="plaintext">).*?(?=</td>)',
                 isP=True,
                 data='hash%5B_input_%5D=FUZZ&hash%5B_csrf_token%5D=')      
    self.rq.setopt(pycurl.REFERER,'http://www.c0llision.net/webcrack.php')  #需要设置reffer

  def genDataSytax(self):                    #生成传送的数据
    setDataP=re.compile(r'FUZZ')              #替换
    encData=setDataP.sub(self.enc,self.dataSyx,1)
    
    perRq=pycurl.Curl()       #生成网络连接获取csrf_token数据
    
    perRq.setopt(pycurl.FOLLOWLOCATION, 1)
    perRq.setopt(pycurl.MAXREDIRS, 5)  #重定向次数
    perRq.setopt(pycurl.CONNECTTIMEOUT,60)  #连接超时
    perRq.setopt(pycurl.TIMEOUT,300)    #下载超时
    perRq.setopt(pycurl.USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0')   #模拟浏览器

    perRq.setopt(pycurl.COOKIEFILE,'')  #设置cookie,开启cookie
    perRq.setopt(pycurl.URL,'http://www.c0llision.net/webcrack.php')  #从这里获取数据
    perRq.setopt(pycurl.WRITEFUNCTION,self.__saveResponse)
    self.getDataHtml=""
    perRq.perform()        #从这里获取csrf_token
    hCode=perRq.getinfo(perRq.HTTP_CODE)
    if hCode==200:
      tokenDataP=re.compile(r'(?<=name="hash\[_csrf_token\]" value=").*?(?=" id="hash__csrf_token")') #寻找csrf_token
      tokenData=tokenDataP.search(self.getDataHtml)
      tokenD=tokenData.group(0)          #生成csrf_token
     
      cookie=perRq.getinfo(pycurl.INFO_COOKIELIST)         #寻找cookie     
      cookieList=cookie[0].split('\t')     
      self.rq.setopt(pycurl.COOKIE,cookieList[5]+'='+cookieList[6])  #生成cookie文件
     
    return encData+tokenD    
      
  def __saveResponse(self,htmlData):
    self.getDataHtml+=htmlData      